/* GSStocksQueryListAction.java
 *
 * Project/Product: JZCG-XMBUS
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-8-26	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.util.ArrayList;

import javax.mail.Session;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSOrganizationProcessor;
import com.hitrust.gs.business.GSStocksProcesser;
import com.hitrust.gs.dao.GSOrderDAO;
import com.hitrust.gs.form.GSOrderDetailQueryForm;
import com.hitrust.gs.form.GSStocksFindForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSStocksVO;

public class GSStocksQueryListAction extends GSBaseAction {
    private GSStocksProcesser sproce=new GSStocksProcesser();
    private GSOrganizationProcessor orpro=new GSOrganizationProcessor();
    public String execute(GSBaseForm aForm) throws Exception {
        // TODO Auto-generated method stub
        GSStocksFindForm stform=(GSStocksFindForm) aForm;
        //取得操作员
        GSOperatorBean oper=(GSOperatorBean) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        ArrayList operarr=orpro.getOrgSub(oper.getGs_oper_org_id());
        aForm.getRequest().setAttribute("orgship", operarr);
      //取得运营公司
        ArrayList ar=orpro.getOrgWarehouse();
        aForm.getRequest().setAttribute("orgware", ar);
        try {
            //转换为vo类对象
            GSStocksVO svo=new GSStocksVO();
            svo.gs_manu_catid1=stform.getManuftype1();
            svo.gs_manu_catid2=stform.getManuftype2();
            svo.gs_manu_catid3=stform.getManuftype3();
            svo.gs_stocks_id=stform.getStocksid();
            String wid="";
            if(!stform.getOrderDeptId().equals("0000")){
                wid=stform.getOrderDeptId();
            }
            String sta=null;
            if (!stform.getStocksstate().equals("")) {
                sta=stform.getStocksstate();
            }
            svo.gs_workcorp_id=wid;
            svo.gs_stocks_state=sta;
            svo.gs_stocks_person=stform.getStocksperson();
            svo.gs_stocks_dept=stform.getStocksdept();
            svo.gs_stocks_datestart=stform.getStocksstarttime();
            svo.gs_stocks_dateend=stform.getStocksendtime();
            svo.gs_manuname=stform.getStocksmanuname();
            svo.gs_manuconid=stform.getStocksconid();
            svo.gs_proid=stform.getProviderid();
            if (stform.getShipid().equals("0000")) {
                svo.arr=operarr;
            }else{
                svo.gs_shipment_id=stform.getShipid();
            }
            
            
            //取得分页参数
            
            String[] tStr = this.getPageParams(stform,svo);
            
            //执行分页查询
            ArrayList tList = sproce.getStocksResultByPage(svo, Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
            ResultProcess tRsProc = new ResultProcess();
            tRsProc.setResult(tList);
            stform.getRequest().setAttribute("stocks", tRsProc);
            
            //存储分页参数
            stform.getRequest().setAttribute("pageSize",tStr[1]);
            stform.getRequest().setAttribute("totalRecords",tStr[2]);
          //种类等级
            GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
            tGSManufProcessor.getManufactureOfLevel(aForm);
        } catch (Exception e) {
            aForm.getRequest().setAttribute("action", "A181301.do");
            throw e;
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
    private String[] getPageParams(GSStocksFindForm aForm,GSStocksVO svo) throws Exception{
        String tStr[] = new String[3];
        int totalRecords;
        int pageNo;
        int pageSize;
        String tPage = GSBaseConstants.tPage;
        //根据页面传入的查询参数查询订单信息
        if(aForm.getChoice() == null || aForm.getChoice().equals("")){
            totalRecords = sproce.getStocksResultCount(svo);
            
            // 取得系统默认的页大小
            pageSize = Integer.parseInt(tPage);
            // PageNo=1
            pageNo = 1;
            int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
            if (pageNo>n) {
                pageNo=n;
            }
            tStr[0] = "" + pageNo;
            tStr[1] = "" + pageSize;
            tStr[2] = "" + totalRecords;
        }else{
            tStr = GSBaseManager.getPageNoAndPageSize(aForm);
        }
        return tStr;
    }
}
